package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.comic.common.threadlocal.SessionLoad;
import com.nifostasky.comic.entitys.SysUser;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.mapper.CoDetailedInfoMapper;
import com.nifostasky.comic.entitys.CoSpiderInfo;
import com.nifostasky.comic.mapper.CoSpiderInfoMapper;
import com.nifostasky.comic.query.CoSpiderInfoQuery;
import com.nifostasky.comic.service.CoDetailedInfoService;
import com.nifostasky.comic.service.CoSpiderInfoService;
import com.nifostasky.comic.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 爬取网页python文件信息
 * (CoSpiderInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-02-13 15:00:45
 */
@Service("coSpiderInfoService")
public class CoSpiderInfoServiceImpl extends ServiceImpl<CoSpiderInfoMapper, CoSpiderInfo> implements CoSpiderInfoService {
    @Autowired
    private CoDetailedInfoService coDetailedInfoService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CoSpiderInfoService coSpiderInfoService;
    public Map<String,Object> queryCoSpiderInfoPage(Integer pageIndex, Integer pageSize, CoSpiderInfoQuery query){
        Map<String,Object> map = new HashMap<>();
        if (Objects.isNull(pageIndex)) pageIndex = 1;
        if (Objects.isNull(pageSize)) pageSize = 10;
        Page<CoSpiderInfo> page = this.page(new Page<CoSpiderInfo>(pageIndex, pageSize), new QueryWrapper<CoSpiderInfo>().lambda()
                .eq(Objects.nonNull(query.getUserId()), CoSpiderInfo::getUserId, query.getUserId())
                .like(StringUtils.isNotBlank(query.getKeyword()), CoSpiderInfo::getName, query.getKeyword())
                .orderByAsc(CoSpiderInfo::getSort, CoSpiderInfo::getCreateDate)
        );
        map.put("total", page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }

    @Override
    public List<CoSpiderInfo> queryCoSpiderInfoList(CoSpiderInfoQuery query) {
        List<CoSpiderInfo> list = this.list(new QueryWrapper<CoSpiderInfo>().lambda()
                .eq(Objects.nonNull(query.getUserId()), CoSpiderInfo::getUserId, query.getUserId())
                .like(StringUtils.isNotBlank(query.getKeyword()), CoSpiderInfo::getName, query.getKeyword())
                .orderByAsc(CoSpiderInfo::getSort, CoSpiderInfo::getCreateDate)
        );
        return list;
    }

    @Override
    public Integer addCoSpiderInfo(CoSpiderInfo info) {
        if (Objects.isNull(info))
            throw new BusinessException("新增信息不能为空");
        info.setUserId(SessionLoad.getLocalData().getUserId());
        info.setUserName(SessionLoad.getLocalData().getUserName());
        this.save(info);
        return 1;
    }

    @Override
    public Integer deleteCoSpiderInfo(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids))
            return 2;
        this.removeByIds(ids);
        return 1;
    }

    @Override
    public CoSpiderInfo queryDefaultSpiderInfo() {
        CoSpiderInfo defaultSpiderInfo = null;
        //判断用户是否已设置默认源
        if (Objects.isNull(SessionLoad.getLocalData().getUserId())){
            defaultSpiderInfo = this.getOne(new QueryWrapper<CoSpiderInfo>().lambda()
                    .eq(CoSpiderInfo::getSort, 0)
            );
        }else{
            SysUser userInfo = sysUserService.getById(SessionLoad.getLocalData().getUserId());
            if (Objects.isNull(userInfo) || Objects.isNull(userInfo.getSpiderId())){
                defaultSpiderInfo = this.getOne(new QueryWrapper<CoSpiderInfo>().lambda()
                        .eq(CoSpiderInfo::getSort, 0)
                );
            }else{
                defaultSpiderInfo = this.getById(userInfo.getSpiderId());
            }
        }
        return defaultSpiderInfo;
    }
}